package com.jeasonzhao.report.msg;

import com.jeasonzhao.commons.logger.BasicLogger;
import com.jeasonzhao.commons.logger.LogLevel;
import com.jeasonzhao.commons.logger.MessageBasedLogListener;
import com.jeasonzhao.report.model.Report;

public abstract class ReportRuntimeLogger extends BasicLogger
{
    private Report m_report = null;
    private boolean m_isVerbose = false;
    public ReportRuntimeLogger(Report report,boolean isVerbose)
    {
        super();
        m_report = report;
        this.m_isVerbose = isVerbose;
    }

    public void log(LogLevel logLevel,String strMessage,Exception excep)
    {

        String strkey = null;
        switch(logLevel)
        {
            case SYSTEM:
                strkey = MessageCodes.LOGLEVEL_SYSTEMINFO;
                break;
            case FATAL:
                strkey = MessageCodes.LOGLEVEL_FATAL;
                break;
            case ERROR:
                strkey = MessageCodes.LOGLEVEL_ERROR;
                break;
            case WARN:
                strkey = MessageCodes.LOGLEVEL_WARNNING;
                break;
            case INFO:
                strkey = MessageCodes.LOGLEVEL_INFO;
                break;
            case DEBUG:
                strkey = MessageCodes.LOGLEVEL_DEBUG;
                break;
            default:
                strkey = MessageCodes.LOGLEVEL_INFO;
                break;
        }
        strkey = MessageCodes.get(strkey);
        String strPrefix = null == m_report ? "[Unknown Report]" : "[" + m_report.getTitle() + "]";
        strMessage = MessageBasedLogListener.formatMessage(strkey,strPrefix,strMessage,excep);
        if(m_isVerbose)
        {
            System.out.println(strMessage);
        }
        writeMessage(strMessage);
    }

    public abstract void writeMessage(String s);

    public static class ConsoleReportRuntimeLogger extends ReportRuntimeLogger
    {
        public ConsoleReportRuntimeLogger(Report report)
        {
            super(report,true);
        }

        public void writeMessage(String s)
        {

        }
    }
}
